package com.example.payment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.payment.entity.Payment;
import com.example.payment.mapper.PaymentMapper;
import com.example.payment.service.PaymentService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.UUID;

@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, Payment> implements PaymentService {
    
    @Override
    @GlobalTransactional
    public Payment pay(String orderNo, Long userId, BigDecimal amount) {
        Payment payment = new Payment();
        payment.setOrderNo(orderNo);
        payment.setUserId(userId);
        payment.setAmount(amount);
        payment.setPaymentNo(UUID.randomUUID().toString().replace("-", ""));
        payment.setStatus(1); // 支付成功
        payment.setCreateTime(LocalDateTime.now());
        payment.setUpdateTime(LocalDateTime.now());
        save(payment);
        return payment;
    }
    
    @Override
    public Payment getPaymentByOrderNo(String orderNo) {
        return getOne(new QueryWrapper<Payment>().eq("order_no", orderNo));
    }
}
